package com.baizhi.zyj.service;

import com.baizhi.zyj.dao.AdminDao;
import com.baizhi.zyj.entity.Admin;
import com.baizhi.zyj.util.ImageCodeUtil;
import com.baizhi.zyj.util.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void register(Admin admin) {
        String password = admin.getPassword();
        //调工具类  获取随机盐
        String salt = Md5Utils.getSalt(6);
        //对原始密码  加随机盐
        String saltPwd = salt + password + salt;
        //对加随机盐的密码进行加密
        String md5Code = Md5Utils.getMd5Code(saltPwd);
        //对传过来的数据进行加工
        admin.setPassword(md5Code);
        admin.setSalt(salt);
        adminDao.insert(admin);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, String> getImageCodes() throws IOException {
        //生成验证码
            Map<String,String> map =new HashMap<>();
                //调用工具类 获取随机验证码字符
                String code = ImageCodeUtil.getSecurityCode();
                //调用工具类 根据随机码生成图片base64的形式
                String imgBase64 = ImageCodeUtil.careateImgBase64(code);
                //把base64 图片信息存放map
                map.put("imgCode", imgBase64);
                //获取uuid
                String uuid = UUID.randomUUID().toString();
                map.put("token", uuid);
                //System.out.println(uuid);

                //往redis中存验证码
                redisTemplate.setKeySerializer(new StringRedisSerializer());
                ValueOperations valueOperations = redisTemplate.opsForValue();
                valueOperations.set("CODE_" + uuid, code, 1, TimeUnit.MINUTES);
                return map;
        }

    @Override
    public Map<String, String> login(@RequestBody Admin admin, String token) {
        Map<String,String> map = new HashMap<>();
        //从redis中  获取服务器上的验证码
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object obj = valueOperations.get("CODE_" + token);
        //检查验证码
        if(obj == null){//验证码过期
            map.put("message","验证码过期");
            map.put("state","error");
            return map;
        }
        String serverCode = obj.toString();
        if (!admin.getEnCode().equals(serverCode)){//验证码错误
            map.put("message","验证码错误");
            map.put("state","error");
            return map;
        }
        //验证码正确时检查账号密码
        Admin user = adminDao.selectByUsername(admin.getUsername());
        if(user==null){
            map.put("message","账号为空");
            map.put("state","error");
            return map;
        }else {//账号存在
            if(admin.getStatus()=="0"){//如果用户状态是0 冻结
                map.put("message","用户已被冻结");
                map.put("state","error");
                return map;
            }
                //获取输入的密码
                //加DB相同的盐
                String saltpwd = user.getSalt() + user.getPassword() + user.getSalt();
                //对加盐后的密码 md5
                if (!saltpwd.equals(user.getPassword())) {//ok
                    map.put("state", "success");
                    map.put("message", token);
                    valueOperations.set("TOKEN_" + token, user);
                } else {
                    map.put("message", "密码错误");
                    map.put("state", "error");
                    return map;
                }
        }
        return map;
    }

    //登录获取用户信息
    @Override
    public Admin getUserInfo(String token) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        return (Admin) valueOperations.get("TOKEN_"+token);
    }

    //退出
    @Override
    public void logout(String token) {
        //从redis中删除数据
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.delete("TOKEN_"+token);

    }



}


